package homework5;

abstract class Animal{
	private String nickName;
	private int age;
	private String color;
	public Animal() {
		
	}
	
	
	
	public Animal(String nickName, int age, String color) {
		super();
		this.nickName = nickName;
		this.age = age;
		this.color = color;
	}
	public String getNickName() {
		return nickName;
	}
	public void setNickName(String nickName) {
		this.nickName = nickName;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getColor() {
		return color;
	}
	public void setColor(String color) {
		this.color = color;
	}
	public abstract void shout();
	public abstract void eat();
	
	
}




  class Dog extends Animal{
	 public Dog() {
		// TODO Auto-generated constructor stub
	}

	public Dog(String nickName, int age, String color) {
		super(nickName, age, color);
		// TODO Auto-generated constructor stub
	}


	@Override //方法重写
	public void shout() {
		System.out.println("狗狗的叫声   汪 汪  汪 。。。");
		
	}

	@Override
	public void eat() {
		System.out.println("狗狗吃骨头。。");
	}
	//扩展一个自己的特殊方法
	public void lookHome() {
		
		System.out.println("狗狗看门");
	}
  
  }
  
  class Cat extends Animal{
	  public Cat() {
		  
	  }

	  
	  
	public Cat(String nickName, int age, String color) {
		super(nickName, age, color);
		
	}



	@Override
	public void shout() {
		System.out.println("喵喵的叫声：喵 喵 喵。。");
		
	}

	@Override
	public void eat() {

       System.out.println("猫吃鱼...");
		
	}
	//扩展一个自己特有的方法
	public void catchMouse() {
		System.out.println("猫捉老鼠");
	}
	  
	  
  }
  
  class Master{
	  private String name;
	  public Master() {
		  
	  }
	  public Master(String name) {
		  this.name=name;
	  }
	  public void feed(Animal animal) {
		  animal.shout();
		  animal.eat();
	  }
	  //instance of 运算符作用是用来判断类型
	  //防止出现ClassCastExceotion 类型转换异常
		 private static void work(Animal a) {
			 /*
			  * 对象 instanceof 类类型或接口类型
			  * 
			  * */
			 
			 
			 
			 
			 if(a instanceof Dog) {
				 
				 //int a=(int)3.14;
				 Dog d=(Dog)a;
				 d.lookHome();
				 
			 }
			 else if(a instanceof Cat) {
				 Cat c=(Cat)a;
				 c.catchMouse();
			 }
			 
		 } 
  

  }
  public class Text {
	
	

	public static void main(String[] args) {
		Master master =new Master("老李");
		Dog dog =new Dog("小黑",2,"黑色");
		Cat cat =new Cat("小花",3,"棕色");
		master.feed(dog);
		master.feed(cat);
		
		/*子类对象赋值给父类引用     --向上转型
		 * 
		 * */
		
		Dog dog2 =new Dog("旺财",3,"黄色");
		dog2.shout();
		dog2.eat();
		 
		/*父类引用转型为具体子类对象-向下转型*/
		Dog dog3=(Dog)dog2;//向下转型

	}

}
